package com.cdm.domain.service;

import com.cdm.domain.dao.SequenceDAO;
import com.cdm.persistence.Sequence;

public class SequenceServiceImpl implements SequenceService {
	private SequenceDAO sequenceDAO = new SequenceDAO();

	public long getSequence(String name) {
		Sequence sequence = null;
		try {
//			sequenceDAO.beginTransaction();
			sequence = sequenceDAO.get(name);
			if (null == sequence) {
				sequence = new Sequence(name, 0l);
				sequenceDAO.save(sequence);
			} else {
				sequence.setValue(sequence.getValue() + 1);
				sequenceDAO.update(sequence);
			}
//			sequenceDAO.commit();
		} catch (RuntimeException e) {
			e.printStackTrace();
//			sequenceDAO.rollback();
		}
		return sequence.getValue();
	}

}
